package com.nbicc.hbanner;

import android.Manifest;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ViewSwitcher;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.lake.banner.transformer.FadePageTransformer;
import com.lake.banner.view.BannerViewPager;
import com.lake.banner.view.ViewPagerScroller;
import com.lake.hbanner.HBanner;
import com.lake.hbanner.ImageSubView;
import com.lake.hbanner.SubView;
import com.lake.hbanner.VideoSubView;
import com.lake.hbanner.VideoViewType;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FullScreenActivity extends BaseActivity implements ViewTreeObserver.OnGlobalLayoutListener {
    private static final String TAG = "FullScreenActivity";
    Context mContext;
    BannerViewPager viewPager;
    private HBanner hBanner;
    private static final float MIN_ALPHA = 0.0f;    //最小透明度
    ViewPagerScroller scroller;
    private View rootView;
    RelativeLayout pause, setting;
    ImageSwitcher background;
    ImageView backImage;
    ViewSwitcher.ViewFactory backFactory;
    boolean isPlay = true;
    boolean isShow = false;
    private static int DUR = 15000;
    private static int AN_DUR = 4000;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        rootView = LayoutInflater.from(this)
                .inflate(R.layout.activity_full, null);
        setContentView(rootView);
        scroller = new ViewPagerScroller(this);
        viewPager = findViewById(R.id.viewpager);
        pause = findViewById(R.id.pause);
        setting = findViewById(R.id.setting_layout);
        background = findViewById(R.id.background);
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
    }

    private void initViews() {
        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (hBanner != null) {
                    hBanner.resume(true);
                    isPlay = true;
                    pause.setVisibility(View.GONE);
                }
            }
        });
        hBanner = HBanner.create(viewPager);
        backFactory = new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                backImage = new ImageView(mContext);
                backImage.setScaleType(ImageView.ScaleType.FIT_XY);
                backImage.setLayoutParams(new ImageSwitcher.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                return backImage;
            }
        };
        background.setFactory(backFactory);
        Animation backinAnimation = AnimationUtils.loadAnimation(mContext, R.anim.fade_in);
        Animation backoutAnimation = AnimationUtils.loadAnimation(mContext, R.anim.fede_out);
        backinAnimation.setDuration(AN_DUR);
        backoutAnimation.setDuration(AN_DUR);
        background.setInAnimation(backinAnimation);
        background.setOutAnimation(backoutAnimation);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.image1);
        Bitmap blur = TiorbBlurBitmapUtil.instance().blurBitmap(mContext, bitmap, 5, bitmap.getWidth(), bitmap.getHeight());
        background.setImageDrawable(new BitmapDrawable(blur));
        //设置viewpager切换方式
        viewPager.setPageTransformer(true, new FadePageTransformer());
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Log.d(TAG, "onPageScrollStateChanged:====>" + state);

            }
        });
        viewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        });
        scroller.initViewPagerScroll(viewPager);
        scroller.setScrollDuration(AN_DUR * 2);
        startPlay();

    }

    private void startPlay() {
        hBanner.sources(initDatas());
        hBanner.play(true);
    }

    private List<SubView> initDatas() {
        List<SubView> data = new ArrayList<>();
        data.add(new ImageSubView.Builder(getBaseContext())
                .url("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4148675854,1608370142&fm=26&gp=0.jpg")
                .gravity(ImageView.ScaleType.FIT_CENTER)
                .duration(6000)
                .build());
        data.add(new ImageSubView.Builder(getBaseContext())
                .gravity(ImageView.ScaleType.FIT_CENTER)
                .resId(R.mipmap.b2)
                .duration(5000)
                .build());
        data.add(new ImageSubView.Builder(getBaseContext())
                .gravity(ImageView.ScaleType.FIT_CENTER)
                .resId(R.mipmap.b3)
                .duration(5000)
                .build());
        data.add(new VideoSubView.Builder(getBaseContext())
                .url("http://vfx.mtime.cn/Video/2019/03/19/mp4/190319222227698228.mp4")
                .gravity(VideoViewType.FULL)
                .isSub(false)
                .build());
        data.add(new ImageSubView.Builder(getBaseContext())
                .gravity(ImageView.ScaleType.FIT_CENTER)
                .resId(R.mipmap.image1)
                .duration(5000)
                .build());
        data.add(new ImageSubView.Builder(getBaseContext())
                .gravity(ImageView.ScaleType.FIT_CENTER)
                .resId(R.mipmap.image2)
                .duration(5000)
                .build());
        data.add(new VideoSubView.Builder(getBaseContext())
                .gravity(VideoViewType.CENTER)
                .file(new File("/sdcard/Tiorb/SULRIW/p_0_video"))
                .build());
        data.add(new ImageSubView.Builder(getBaseContext())
                .gravity(ImageView.ScaleType.FIT_CENTER)
                .resId(R.mipmap.image3)
                .duration(5000)
                .build());
        data.add(new ImageSubView.Builder(getBaseContext())
                .gravity(ImageView.ScaleType.FIT_CENTER)
                .resId(R.mipmap.image4)
                .duration(5000)
                .build());
        data.add(new ImageSubView.Builder(getBaseContext())
                .gravity(ImageView.ScaleType.FIT_CENTER)
                .file(new File("/sdcard/Tiorb/ATUTII/p_0"))
                .duration(5000)
                .build());
        data.add(new ImageSubView.Builder(getBaseContext())
                .gravity(ImageView.ScaleType.FIT_CENTER)
                .resId(R.mipmap.image5)
                .duration(5000)
                .build());
        data.add(new VideoSubView.Builder(getBaseContext())
                .url("http://vfx.mtime.cn/Video/2019/03/18/mp4/190318214226685784.mp4")
                .gravity(VideoViewType.FULL)
                .isSub(false)
                .build());
        data.add(new ImageSubView.Builder(getBaseContext())
                .gravity(ImageView.ScaleType.FIT_CENTER)
                .resId(R.mipmap.image6)
                .duration(5000)
                .build());
        data.add(new ImageSubView.Builder(getBaseContext())
                .gravity(ImageView.ScaleType.FIT_CENTER)
                .resId(R.mipmap.image7)
                .duration(5000)
                .build());
        data.add(new ImageSubView.Builder(getBaseContext())
                .gravity(ImageView.ScaleType.FIT_CENTER)
                .resId(R.mipmap.image8)
                .duration(5000)
                .build());
        return data;
    }


    @Override
    void afterPermissions() {
        initViews();
    }

    @Override
    String[] needPermissions() {
        return new String[0];
    }

    @Override
    protected void onResume() {
        if (hBanner != null)
            hBanner.play(true);
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (hBanner != null)
            hBanner.pause(0);
        super.onPause();
    }

    @Override
    protected void onStop() {
        if (hBanner != null)
            hBanner = null;
        super.onStop();
    }

    @Override
    public void onGlobalLayout() {
        rootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        verifyStoragePermissions(FullScreenActivity.this);
    }

    GestureDetector gestureDetector = new GestureDetector(mContext, new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            Log.d(TAG, "单击屏幕");
            if (isShow) {
                setting.setVisibility(View.INVISIBLE);
                isShow = false;
            } else {
                setting.setVisibility(View.VISIBLE);
                isShow = true;
            }
            return super.onSingleTapConfirmed(e);
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.d(TAG, "双击屏幕");
            if (isPlay) {
                hBanner.pause(0);
                isPlay = false;
                pause.setVisibility(View.VISIBLE);
            } else {
                hBanner.resume(true);
                isPlay = true;
                pause.setVisibility(View.GONE);
            }

            return super.onDoubleTap(e);
        }
    });


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == 1) {
            return false;
        }
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_CENTER:
                if (isPlay) {
                    hBanner.pause(0);
                    isPlay = false;
                    pause.setVisibility(View.VISIBLE);
                } else {
                    hBanner.resume(true);
                    isPlay = true;
                    pause.setVisibility(View.GONE);
                }
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT://左
                if (hBanner != null) {
                    hBanner.showPreview(true);
                }
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT://右
                if (hBanner != null) {
                    hBanner.showNext(true);
                }
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}
